package neu.edu.vn.appmusic.adapter;

import java.util.ArrayList;
import java.util.List;

import neu.edu.vn.appmusic.R;
import neu.edu.vn.appmusic.object.Album;
import neu.edu.vn.appmusic.utils.ImageUtils;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class AlbumApdater extends ArrayAdapter {
	private ArrayList<Album> listAlbum;
	private Context mContext;
	private int layoutId;

	public AlbumApdater(Context context, int resource, ArrayList<Album> objects) {
		super(context, resource, objects);
		mContext = context;
		listAlbum  = objects;
		layoutId = resource;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		if(convertView == null){
			viewHolder = new ViewHolder();
			LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(layoutId, null);
			viewHolder.ivAlbum = (ImageView) convertView.findViewById(R.id.ivItemAlbum);
			viewHolder.tvAlbumName = (TextView) convertView.findViewById(R.id.tvAlbumName);
			viewHolder.tvSinger = (TextView) convertView.findViewById(R.id.tvSinger);
			convertView.setTag(viewHolder);
		}else{
			viewHolder = (ViewHolder) convertView.getTag();
		}
		
		Album album = listAlbum.get(position);
		if(album !=null){
			viewHolder.tvAlbumName.setText(album.getAlbumName());
			viewHolder.tvSinger.setText(album.getAlbumSinger());
			ImageUtils.getInstance(mContext).loadImageToImageview(album.getAlbumImage(), viewHolder.ivAlbum);
		}
		
		return convertView;
		
	}
	
	static class ViewHolder{
		ImageView ivAlbum;
		TextView tvAlbumName;
		TextView tvSinger;
	}
	
	

}
